﻿namespace SimulChess
{
    public class Pawn : ChessPiece
    {
        public Pawn(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.pawn, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 1;
            base.Upgradeable = true;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if (base.Color == PieceColor.white)
                    {
                        if ((x == 6) && (y == 6)) base.MoveMatrix[x, y] = Movement.capture;
                        if ((x == 7) && (y == 6)) base.MoveMatrix[x, y] = Movement.allowed;
                        if ((x == 8) && (y == 6)) base.MoveMatrix[x, y] = Movement.capture;
                        if ((x == 7) && (y == 5)) base.MoveMatrix[x, y] = Movement.allowedOnce;
                    }
                    else
                    {
                        if ((x == 6) && (y == 8)) base.MoveMatrix[x, y] = Movement.capture;
                        if ((x == 7) && (y == 8)) base.MoveMatrix[x, y] = Movement.allowed;
                        if ((x == 8) && (y == 8)) base.MoveMatrix[x, y] = Movement.capture;
                        if ((x == 7) && (y == 9)) base.MoveMatrix[x, y] = Movement.allowedOnce;
                    }
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = new bool[8, 8];

            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    int relativeX = base.PositionX + (x - 7);
                    int relativeY = base.PositionY + (y - 7);

                    if ((relativeX < 0) || (relativeY < 0) || (relativeX > 7) || (relativeY > 7))
                        continue;

                    moveArray[relativeX, relativeY] = false;

                    if (((Board.Position[relativeX, relativeY].Occupied == false)
                            || ((base.MoveMatrix[x, y] == Movement.allowed) || (base.MoveMatrix[x, y] == Movement.capture) || (base.MoveMatrix[x, y] == Movement.both)))
                        && ((base.MoveMatrix[x, y] != Movement.blocked)
                            || ((base.HasMoved == false) && (base.MoveMatrix[x, y] == Movement.allowedOnce))))
                    {
                        moveArray[relativeX, relativeY] = true;
                    }
                }
            }

            return moveArray;
        }
    }

    public class Knight : ChessPiece
    {
        public Knight(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.knight, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 3;
            base.CanJump = true;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if ((x == 5) && (y == 8)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 6) && (y == 9)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 8) && (y == 9)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 9) && (y == 8)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 5) && (y == 6)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 6) && (y == 5)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 8) && (y == 5)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 9) && (y == 6)) base.MoveMatrix[x, y] = Movement.both;
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = new bool[8, 8];

            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    int relativeX = base.PositionX + (x - 7);
                    int relativeY = base.PositionY + (y - 7);

                    if ((relativeX < 0) || (relativeY < 0) || (relativeX > 7) || (relativeY > 7))
                        continue;

                    moveArray[relativeX, relativeY] = false;

                    if (base.MoveMatrix[x, y] != Movement.blocked)
                    {
                        moveArray[relativeX, relativeY] = true;
                    }
                }
            }

            return moveArray;
        }
    }

    public class Bishop : ChessPiece
    {
        public Bishop(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.bishop, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 3;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if ((x == y) || ((x + y) == 14)) base.MoveMatrix[x, y] = Movement.both;
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = base.ShowMoves(Board);

            return moveArray;
        }
    }

    public class Rook : ChessPiece
    {
        public Rook(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.rook, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 5;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if ((x == 7) || (y == 7)) base.MoveMatrix[x, y] = Movement.both;
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = base.ShowMoves(Board);

            return moveArray;
        }
    }

    public class Queen : ChessPiece
    {
        public Queen(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.queen, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 9;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if ((x == y) || ((x + y) == 14)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 7) || (y == 7)) base.MoveMatrix[x, y] = Movement.both;
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = base.ShowMoves(Board);

            return moveArray;
        }
    }

    public class King : ChessPiece
    {
        public King(PieceColor PlayerColor, int X, int Y)
            : base(PieceName.king, PlayerColor)
        {
            base.PositionX = X;
            base.PositionY = Y;

            base.RelativeValue = 0;
        }

        protected override void SetMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    if ((x == 6) && (y == 8)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 7) && (y == 8)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 8) && (y == 8)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 6) && (y == 7)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 8) && (y == 7)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 6) && (y == 6)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 7) && (y == 6)) base.MoveMatrix[x, y] = Movement.both;
                    if ((x == 8) && (y == 6)) base.MoveMatrix[x, y] = Movement.both;
                }
            }
            base.MoveMatrix[7, 7] = Movement.allowed;
        }

        public override bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = base.ShowMoves(Board);

            return moveArray;
        }
    }
}